﻿using System.Collections.Generic;
using Books.Model;

namespace Books.Web.ViewModels
{
    public sealed class PublisherDetailViewModel
    {
        public static PublisherDetailViewModel FromModel(PublisherModel model)
        {
            var vm = new PublisherDetailViewModel();
            vm.Publisher = new PublisherDetailViewModel.PublisherViewModel()
                {
                    City = model.City,
                    PublisherId = model.PublisherId,
                    PublisherName = model.PublisherName
                };

            foreach (var book in model.Books)
            {
                var bookvm = new BookViewModel()
                    {
                       BookId = book.BookId,
                       PublishedYear = book.PublishedYear.HasValue ? book.PublishedYear.Value.ToString() : "",
                       Title = book.Title
                    };

                foreach (var author in book.Authors)
                {
                    bookvm.Authors.Add(new AuthorViewModel()
                        {
                            AuthorId = author.AuthorId,
                            Born = author.Born.HasValue ? author.Born.Value.ToString() : "",
                            Died = author.Died.HasValue ? author.Died.Value.ToString() : "",
                            FullName = author.GetFullName()
                        });
                }

                vm.Books.Add(bookvm);
            }

            foreach (var author in model.Authors)
            {
                vm.Authors.Add(new AuthorViewModel()
                    {
                        AuthorId = author.AuthorId,
                        Born = author.Born.HasValue ? author.Born.Value.ToString() : "",
                        Died = author.Died.HasValue ? author.Died.Value.ToString() : "",
                        FirstName = author.FirstName,
                        LastName = author.LastName
                    });
            }

            return vm;
        }

        public class AuthorViewModel
        {
            public int AuthorId { get; set; }
            public string Born { get; set; }
            public string Died { get; set; }
            public string FirstName { get; set; }
            public string FullName { get; set; }
            public string LastName { get; set; }
        }

        public class BookViewModel
        {
            public IList<AuthorViewModel> Authors = new List<AuthorViewModel>();
            public int BookId { get; set; }
            public string PublishedYear { get; set; }
            public string Title { get; set; }
        }

        public class PublisherViewModel
        {
            public string City { get; set; }
            public int PublisherId { get; set; }
            public string PublisherName { get; set; }
        }

        public IList<AuthorViewModel> Authors = new List<AuthorViewModel>();
        public IList<BookViewModel> Books = new List<BookViewModel>();
        public PublisherViewModel Publisher { get; private set; }

        private PublisherDetailViewModel() { }
    }
}